package com.bw.shoppingmall.wiget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bw.shoppingmall.R;


public class BottomButton extends RelativeLayout implements View.OnClickListener {
    private LinearLayout linearLayout;
    private ImageView imageView;
    private TextView textView;
    private BottomSelectLisenner lisenner;
    private boolean selFlag = false;
    private int nomalsize = ConvertUtils.dp2px(5);
    private int selSize = ConvertUtils.dp2px(7);
    private int nomalColor = Color.BLACK;
    private int selColor = Color.RED;
    private int nomalImg ;
    private int selImg;
    boolean aBoolean;
    private String content = "";

    public BottomButton setContent(String content) {
        this.content = content;
        return this;
    }

    public BottomButton setNomalImg(int nomalImg) {
        this.nomalImg = nomalImg;
        return this;
    }

    public BottomButton setSelImg(int selImg) {
        this.selImg = selImg;
        return this;
    }

    public BottomButton setselColor(int selColor){
        this.selColor = selColor;
        return this;
    }

    public BottomButton setnomalColor(int nomalColor){
        this.nomalColor = nomalColor;
        return this;
    }

    public BottomButton setselSize(int selSize){
        this.selSize = ConvertUtils.sp2px(selSize);
        return this;
    }
    public BottomButton setNomalsize(int namalsize){
        this.nomalsize = ConvertUtils.sp2px(namalsize);
        return this;
    }

    public BottomButton setSelFlag(boolean SelFlag){
        this.selFlag = SelFlag;
        refresh();
        return this;
    }

    public BottomButton setLinearlisenner(BottomSelectLisenner lisenner){
        this.lisenner =lisenner;
        return this;
    }

    public BottomButton(Context context) {
        super(context);
        init();

    }

    public BottomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.bottom_arrs);
        aBoolean = typedArray.getBoolean(R.styleable.bottom_arrs_selflag, false);
        typedArray.recycle();
        init();
    }

    public void init(){
        linearLayout = new LinearLayout(getContext());
        LayoutParams layoutParams = new LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(CENTER_IN_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        imageView = new ImageView(getContext());
        LinearLayout.LayoutParams imglp = new LinearLayout.LayoutParams(ConvertUtils.dp2px(20),ConvertUtils.dp2px(25));
        imageView.setLayoutParams(imglp);
        linearLayout.addView(imageView);
        textView = new TextView(getContext());
        LinearLayout.LayoutParams contenlp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(contenlp);
        linearLayout.addView(textView);
        addView(linearLayout);
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        selFlag = true;
        refresh();
        if (lisenner != null){
            lisenner.onSelect(getId());
            return;
        }
    }

    public void refresh() {
        int imgRes= nomalImg;
        int tvColor = nomalColor;
        int tvsize = nomalsize;
        if (selFlag){
            imgRes = selImg;
            tvColor = selColor;
            tvsize = selSize;

        }
        imageView.setImageResource(imgRes);
        textView.setTextColor(tvColor);
        textView.setTextSize(tvsize);
        textView.setText(content);
    }

    public interface BottomSelectLisenner{
        void onSelect(int id);
    }

}
